from flatfilelayout import FlatFileLayout

class FlatFileRecord(list): #Inherit the list type.  Yay Python.
    def __init__(self, raw_string=None, layout=None):
        if isinstance(raw_string, str) and isinstance(layout, FlatFileLayout):
            self.parseLine(raw_string, layout)
        else:
            pass #what to do here?  I dunno.  Exception wouldn't be appropriate.

    def parseLine(self, raw_string, layout):
        assert isinstance(raw_string, str)
        max_index = len(raw_string) #Know what index we can't go past.
        if max_index == 0: #IF this string is empty, return False and quit now.
            return False
        else:
            max_index = max_index - 1

        for field in layout.getOrderedFields():
            this_first_index = field.getFirstIndex()
            this_last_index = field.getLastIndex()
            self.append(raw_string[this_first_index:this_last_index])            
            
